package com.aidex.web.controller.biz;

import com.aidex.biz.domain.Appointment;
import com.aidex.biz.service.AppointmentService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 上门预约Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-10-11
 */
@RestController
@RequestMapping("/biz/bizAppointment")
public class AppointmentController extends BaseController {
    @Autowired
    private AppointmentService appointmentService;

    /**
     * 查询上门预约列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointment:list')")
    @GetMapping("/list")
    public R<PageInfo> list(Appointment appointment, HttpServletRequest request, HttpServletResponse response) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            appointment.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }

        
        appointment.setPage(new PageDomain(request, response));
        return R.data(appointmentService.findPage(appointment));
    }

    /**
     * 获取上门预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointment:query')")
    @GetMapping(value = "/{id}")
    public R<Appointment> detail(@PathVariable("id") String id) {
        return R.data(appointmentService.get(id));
    }

    /**
     * 新增上门预约
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointment:add')")
    @Log(title = "上门预约", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Appointment appointment) {
        return R.status(appointmentService.save(appointment));
    }

    /**
     * 修改上门预约
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointment:edit')")
    @Log(title = "上门预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Appointment appointment) {
        return R.status(appointmentService.save(appointment));
    }


    /**
     * 删除上门预约
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointment:remove')")
    @Log(title = "上门预约", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(appointmentService.deleteAppointmentByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointment:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(appointmentService.findMaxSort(new Appointment()));
    }

    /**
     * 导出上门预约列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointment:export')")
    @Log(title = "上门预约", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Appointment appointment) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            appointment.setCompanyId(null);
        } else {
            appointment.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<Appointment> list = appointmentService.findList(appointment);
        ExcelUtil<Appointment> util = new ExcelUtil<Appointment>(Appointment.class);
        return util.exportExcel(list, "上门预约数据");
    }

}
